import apiClient from './index';

// 获取积分操作理由响应
export interface PointReasonItem {
    id: number;
    reason: string;
}

export interface PointReasonsResponse {
  code: number;
  content: {
    class_id: number;
    class_name: string;
    reasons: PointReasonItem[];
    total_students: number;
  };
  msg: string;
}

// 更新积分参数
export interface UpdatePointParams {
  student_id: number; // 学生ID
  points_change: number; // 积分变化值（可正可负）
  reason: string; // 操作理由
  admin_password: string; // 教师口令
}

// 更新积分响应
export interface UpdatePointResponse {
  code: number;
  msg: string;
}

/**
 * 获取积分操作预设理由
 */
export const getPointReasons = async (): Promise<PointReasonsResponse> => {
  const response = await apiClient.post<PointReasonsResponse>('/point/reasons');
  return response.data;
};

/**
 * 更新学生积分
 */
export const updatePoint = async (
  params: UpdatePointParams
): Promise<UpdatePointResponse> => {
  const response = await apiClient.post<UpdatePointResponse>('/point/update', params);
  return response.data;
};

// 积分记录相关类型定义
export interface PointRecordItem {
  class_grade: number;
  class_name: string;
  operated_at: string;
  points_change: number;
  reason: string;
  student_id: number;
  student_name: string;
  teacher_id: number;
  teacher_name: string;
  classInfo?: string; // 用于前端展示的年级班级组合
}

export interface PointRecordsParams {
  start: number;
  limit: number;
  start_time?: string;
  end_time?: string;
  class_id?: number;
  student_name?: string;
  student_number?: string;
}

export interface PointRecordsResponse {
  code: number;
  content: {
    limit: number;
    records: PointRecordItem[];
    start: number;
    total: number;
  };
  msg: string;
}

/**
 * 查询积分记录
 */
export const getPointRecords = async (
  params: PointRecordsParams
): Promise<PointRecordsResponse> => {
  const response = await apiClient.post<PointRecordsResponse>('/point/records', params);
  return response.data;
};

// 导出积分记录请求参数
export interface ExportRecordParams {
  class_id?: number;
  group_id?: number;
  student_id?: number;
  start_time?: string;
  end_time?: string;
}

/**
 * 导出积分记录
 */
export const exportPointRecords = async (params: ExportRecordParams) => {
  const response = await apiClient.post('/point/export', params, { responseType: 'blob' });
  return response.data; // 返回blob数据
};

// 积分统计请求参数接口定义
export interface StatisticsParams {
  class_id?: number;
  group_id?: number;
  student_id?: number;
  start_time?: string;
  end_time?: string;
}

export interface ReportParams {
  class_id: number;
  start_time?: string;
  end_time?: string;
}

// 积分统计响应数据接口定义
export interface ReasonItem {
  percentage?: string;
  points: string;
  reason: string;
  personal_percentage?: string;
  group_percentage?: string;
}

export interface StatisticsContent {
  add_reasons: ReasonItem[];
  class_id: number;
  class_name: string;
  grade: number;
  group_id?: number;
  group_name?: string;
  minus_reasons: ReasonItem[];
  total_add: string;
  total_minus: string;
  type: string;
  ratios?: {
    class_add_percentage: number;
    class_minus_percentage: number;
    group_add_percentage: number | null;
    group_minus_percentage: number | null;
  };
  student_id?: number;
  student_name?: string;
}

export interface StatisticsResponse {
  code: number;
  content: StatisticsContent;
  msg: string;
}

export interface ClassInfo {
  class_id: number;
  class_name: string;
  grade: number;
  time_range: {
    end_time: string;
    start_time: string;
  };
  total: string;
  total_add: string;
  total_minus: string;
}

export interface GroupRankingItem {
  group_id: number;
  group_name: string;
  rank: number;
  total: string;
  total_add: string;
  total_minus: string;
}

export interface StudentRankingItem {
  group_id: number | null;
  group_name: string;
  rank: number;
  student_id: number;
  student_name: string;
  total: string;
  total_add: string;
  total_minus: number;
}

export interface ReasonStatsItem {
  add_points: number | string;
  minus_points: number | string;
  reason: string;
}

export interface ReportContent {
  class_info: ClassInfo;
  group_ranking: GroupRankingItem[];
  reason_stats: ReasonStatsItem[];
  student_ranking: StudentRankingItem[];
}

export interface ReportResponse {
  code: number;
  content: ReportContent;
  msg: string;
}

/**
 * 统计积分数据
 */
export const getStatistics = async (
  params: StatisticsParams
): Promise<StatisticsResponse> => {
  const response = await apiClient.post<StatisticsResponse>('/point/statistics', params);
  return response.data;
};

/**
 * 生成积分情况报告数据
 */
export const getReport = async (
  params: ReportParams
): Promise<ReportResponse> => {
  const response = await apiClient.post<ReportResponse>('/point/report', params);
  return response.data;
};